package vn.edu.saigontech.ncms.service.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import vn.edu.saigontech.ncms.model.info.NextOffering;
import vn.edu.saigontech.ncms.service.NextOfferingService;
import vn.edu.saigontech.ncms.util.Checker;

@SuppressWarnings("unchecked")
public class NextOfferingServiceImpl extends HibernateGenericService<NextOffering, Long> implements NextOfferingService {

	@Override
	public List<NextOffering> findByDate(Date fromDate, Date toDate) {
		Criteria criteria = this.createCriteria();
		if(!Checker.isNull(fromDate)) {
			criteria.add(Restrictions.ge("startDate", fromDate));
		}
		if(!Checker.isNull(toDate)) {
			criteria.add(Restrictions.le("startDate", toDate));
		}
		criteria.addOrder(Order.asc("startDate"));
		return criteria.list();
	}
	
	@Override
	public List<NextOffering> findOrderByDate(Date fromDate, Date toDate) {
		Criteria criteria = this.createCriteria();
		if(!Checker.isNull(fromDate)) {
			criteria.add(Restrictions.ge("startDate", fromDate));
		}
		if(!Checker.isNull(toDate)) {
			criteria.add(Restrictions.le("startDate", toDate));
		}
		criteria.addOrder(Order.asc("startDate"));
		criteria.addOrder(Order.asc("endDate"));
		return criteria.list();
	}
}